package cn.takujo.takujoframework.common.util.code_generator;

import java.util.HashMap;
import java.util.Map;

import lombok.Data;

@Data
public class JavaBean {

	private String beanName;
	private String packName;
	private String tableName;
	private PrivateField<?>[] fields;
	private boolean isLombok = false;

	public JavaBean(String beanName, String tableName, String packName, PrivateField<?>[] fields) {
		if (beanName != null) {
			this.beanName = beanName;
		} else {
			String original = tableName;
			if (original.startsWith("t_") || original.startsWith("T_")) {
				original = original.substring(2);
			}
			this.beanName = toHump(original);
		}
		this.packName = packName;
		this.fields = fields;
		this.tableName = tableName;
	}

	public JavaBean(String beanName, String tableName, String packName, PrivateField<?>[] fields, boolean isLombok) {
		if (beanName != null) {
			this.beanName = beanName;
		} else {
			String original = tableName;
			if (original.startsWith("t_") || original.startsWith("T_")) {
				original = original.substring(2);
			}
			this.beanName = toHump(original);
		}
		this.packName = packName;
		this.fields = fields;
		this.isLombok = isLombok;
		this.tableName = tableName;
	}

	@Data
	static class PrivateField<T> {

		private Class<T> type;
		private String name;
		private String column;
		private String firstCase;
		private String typeStr;
		private String qualifiedPackName;

		public PrivateField(Class<T> type, String name, String column) {
			this.type = type;
			this.column = column;
			if (name != null) {
				this.name = name;
			} else {
				String[] split = column.split("_");
				StringBuffer sb = new StringBuffer(split[0]);
				int len = split.length;
				for (int i = 1; i < len; i++) {
					String si = split[i];
					sb.append(si.substring(0, 1).toUpperCase() + si.substring(1));
				}
				this.name = sb.toString();
			}
		}

		public String getTypeStr() {
			typeStr = type.getSimpleName();
			return typeStr;
		}

		public String getQualifiedPackName() {
			qualifiedPackName = packName() + "." + getTypeStr();
			return qualifiedPackName;
		}

		public boolean isLangPack() {
			return "java.lang".equals(packName());
		}

		public String getFirstCase() {
			firstCase = name.substring(0, 1).toUpperCase() + name.substring(1);
			return firstCase;
		}

		private String packName() {
			return type.getPackage().getName();
		}

	}

	private String toHump(String original) {
		String[] split = original.split("_");
		StringBuffer sb = new StringBuffer();
		int len = split.length;
		for (int i = 0; i < len; i++) {
			String si = split[i];
			sb.append(si.substring(0, 1).toUpperCase() + si.substring(1));
		}
		return sb.toString();
	}

	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<>();
		map.put("beanName", beanName);
		map.put("packName", packName);
		map.put("isLombok", isLombok);
		map.put("fields", fields);
		return map;
	}

}
